package com.sczmgk.hotline.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.sczmgk.hotline.R;
import com.sczmgk.hotline.entity.BannerInfo;

import java.util.List;

import static com.sczmgk.hotline.MyApp.bitmapUtils_400;

public class PointAdapter extends PagerAdapter {

    private Context context;
    private List<BannerInfo> lists;

    public PointAdapter(Context context , List<BannerInfo> infos){
        this.context = context ;
        this.lists = infos;
    }

    public void setList(List<BannerInfo> infos){
        this.lists = infos;
    }

    @Override
    public int getCount() {
        return lists == null ? 0 : lists.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    public Object instantiateItem(ViewGroup container, int position) {
        //在此设置背景图片，提高加载速度，解决OOM问题
//            ImageView view = imageViewList.get(position);
        ImageView view = new ImageView(context);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        BannerInfo bannerFindInfo = lists.get(position);
        view.setScaleType(ImageView.ScaleType.FIT_XY);
        //TODO 加载图片
        if ( bannerFindInfo.picurls != null) {
            bitmapUtils_400.display(view,  bannerFindInfo.picurls.largepic);
        }else{
            view.setImageResource(R.drawable.default_center);
        }
        view.setLayoutParams(params);
        container.addView(view, 0);
        return view;
    }
}